import { ERigidBody2DType, EventTouch, log, RigidBody, v2, Vec2, Vec3 } from 'cc';
import { RigidBody2D } from 'cc';
import { instantiate } from 'cc';
import { _decorator, Component, Node, Prefab } from 'cc';
import { LineScript } from './LineScript';
const { ccclass, property } = _decorator;

@ccclass('BowScript')
export class BowScript extends Component {

    @property(Prefab)
    public ArrowPrefab: Prefab

    pitchAngle: number = 0

    pitch :number = 0

    @property(Number)
    velocity :number = 0

    ArrowNode: Node = null

    // start() {

    // }

    // update(deltaTime: number) {
        
    // }


    /**
     * 开火
     */
    public fire(e: EventTouch, velocity :number){
        //瞄准
        this.aim(this.pitchAngle)
        //发射
        this.shoot(velocity)
        log("发射",this.node.angle)
    }

    /**
     * 瞄准
     * @param pitchAngle 
     */
    public aim(pitchAngle: number){
        this.pitch = pitchAngle
    }

    /**
     * 发射
     * @param velocity 
     */
    public shoot(velocity: number){
        //生成箭
        const arrowNode = this.generateBullet()
        const arrowRigiBody :RigidBody2D = arrowNode.getComponent(RigidBody2D)
        //计算速度
        // const direction = arrowNode.forward.negative()
        // direction.normalize()
        // direction.multiplyScalar(velocity)
        // direction.multiplyScalar(velocity)

        // const dir = this.node.position

        // var radian = new Vec2(dir.x, dir.y).signAngle(v2(1,0));
        // var angle = this.node.angle / Math.PI * 180;
        // var angle = this.node.angle * 180 / Math.PI;

        
        //角度计算向量
        const v_x = Math.cos((this.node.angle) * Math.PI / 180) * velocity;
        const v_y = Math.sin((this.node.angle) * Math.PI / 180) * velocity;
        const linearV = v2( v_x, v_y);

        // const line = this.node.parent.getComponentInChildren(LineScript)
        // const newXY = linearV.clone().multiplyScalar(5)
        
        // line.drawLines(v2(this.node.getWorldPosition().x, this.node.getWorldPosition().y) ,v2(this.node.getWorldPosition().x, this.node.getWorldPosition().y).add(newXY))
        
        console.log("linearV",linearV , velocity);
        // console.log("newXY", newXY);

        //给钢体速度
        // arrowRigiBody.linearVelocity = new Vec2(direction.x, direction.y)
        arrowRigiBody.linearVelocity = linearV
        // arrowRigiBody.applyForceToCenter(new Vec2(direction.x, direction.y), true)
        // arrowRigiBody.angularVelocity = this.node.angle
    }

    /**
     * 生成箭
     * @returns 箭对象
     */
    protected generateBullet(){
        const node = instantiate(this.ArrowPrefab)
        // node.angle = this.node.angle
        // console.log("this.node.angle", this.node.angle, this.node.getWorldRotation(), node.getWorldRotation());
        
        this.node.parent.addChild(node)
        //把自己的位置角度都给它
        node.setWorldPosition(this.node.getWorldPosition())
        node.setWorldRotation(this.node.getWorldRotation())
        return node;
    }


    fansile(e: Vec2, e2 :Vec2){
        // if(!this.ArrowNode){
        //     this.ArrowNode = instantiate(this.ArrowPrefab)
        //     this.node.parent.addChild(this.ArrowNode)
        //     this.ArrowNode.setWorldPosition(this.node.getWorldPosition())
        //     this.ArrowNode.getComponent(RigidBody2D).type = ERigidBody2DType.Kinematic
        //     this.ArrowNode.getComponent(RigidBody2D).gravityScale = 0
        // }

        // //把自己的位置角度都给它
        // // this.ArrowNode.setWorldRotation(this.node.getWorldRotation())
        // this.ArrowNode.angle = this.node.angle

        // console.log("ArrowNode.angle", this.ArrowNode.angle)
    }

    shanchu(){
        // this.ArrowNode.destroy()
        // this.ArrowNode = null
    }

}


